//
//  V2MenuSubViewController.m
//  BaseProject
//
//  Created by 王洋 on 2021/4/26.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "V2MenuCombCell.h"
#import "V2MenuSingleCell.h"
#import "V2MenuEmptyCell.h"

#import "V2MenuSectionHeaderView.h"

#import "V2MenuHeaderView.h"
#import "MenuFooterView.h"
#import "V2CommonDishesModel.h"

#import "V2MenuSubViewController.h"

@interface V2MenuSubViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) V2MenuHeaderView *headerView;

@property (nonatomic, strong) MenuFooterView *footerView;

@property (nonatomic, strong) UITableView *tableview;

//把一级拿出来的数组   里面有多个同类的一级   为一个元素
@property (nonatomic, strong) NSMutableArray <V2CommonDishesModel *>* sectionArray;

@property (nonatomic, assign) NSInteger dishesCount;
@end

@implementation V2MenuSubViewController

- (instancetype)initWithModel:(CommonMenuModel *)model {
    self = [super init];
    if (self) {
        self.model = model;
        
        self.sectionArray = @[].mutableCopy;
        self.dishesCount = 0;
        //把各种类型拿出来
        NSMutableArray *dishesTypes = @[].mutableCopy;
        for (V2CommonMenuModel *f_model in self.model.dishesList) {
            if (![dishesTypes containsObject:@(f_model.dishesType)]) {
                [dishesTypes addObject:@(f_model.dishesType)];
            }
        }
        
        //确定有几个分组
        for (int i = 0; i < dishesTypes.count; i++) {
            V2CommonDishesModel *sectionModel = [[V2CommonDishesModel alloc] init];
            sectionModel.array = @[].mutableCopy;
            [self.sectionArray addObject:sectionModel];
        }
        
        //给每个组填充数据
        for (V2CommonMenuModel *f_model in self.model.dishesList) {
            
            if (f_model.dishesList.count == 0) {
                self.dishesCount++;
            } else {
                if (self.sectionArray[f_model.dishesType-1].array.count == 0) {
                    self.dishesCount++;
                }
            }
            
            [self.sectionArray[f_model.dishesType-1].array addObject:f_model];
        }
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    
}

- (void)initView {
    
    self.headerView =  [[NSBundle mainBundle] loadNibNamed:@"V2MenuHeaderView"owner:nil options:nil].firstObject;
    self.headerView.frame = CGRectMake(0, 0, TTScreenWidth, 100);
    self.tableview.tableHeaderView = self.headerView;
    self.headerView.autoresizingMask = UIViewAutoresizingNone;
    self.headerView.headerTitleLab.text = self.model.bhmMenuName;
    self.headerView.headerSubTitleLab.text = [NSString stringWithFormat:@"¥%@/桌 %ld道菜",[Utilities changeDoubleToString:self.model.bhmMenuPrice],(long)self.dishesCount];
    
    self.footerView =  [[NSBundle mainBundle] loadNibNamed:@"MenuFooterView"owner:nil options:nil].firstObject;
    
    CGFloat labHeight = [Utilities getStringHeightWithText:self.model.bhmOrtherText font:[UIFont systemFontOfSize:14] viewWidth:(TTScreenWidth - 70)];
    CGFloat height = MAX(labHeight, 40) + 90;
    
    self.footerView.frame = CGRectMake(0, 0, TTScreenWidth, height);
    self.footerView.itemContentLab.text = self.model.bhmOrtherText;
    self.footerView.serviceFeeLab.text = @"免费";
    self.tableview.tableFooterView = self.footerView;
    self.footerView.autoresizingMask = UIViewAutoresizingNone;
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(14);
        make.right.mas_equalTo(-14);
    }];
    
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.sectionArray.count > 0) {
        return self.sectionArray.count;
    } else {
        return 1;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.sectionArray.count > 0) {
        return self.sectionArray[section].array.count;
    } else {
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.sectionArray.count > 0) {
        if (self.sectionArray[indexPath.section].array[indexPath.row].dishesList.count > 0) {
            NSInteger cellArrayCount = self.sectionArray[indexPath.section].array[indexPath.row].dishesList.count;
            return cellArrayCount * 70 + 30;
        } else {
            return 70;
        }
    } else {
        return TTScreenHeight - 200;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.sectionArray.count > 0) {
        return 40;
    } else {
        return CGFLOAT_MIN;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.whiteColor;
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    V2MenuSectionHeaderView *sectionHeader = [[NSBundle mainBundle] loadNibNamed:@"V2MenuSectionHeaderView"owner:nil options:nil].firstObject;
    switch (section+1) {
        case V2CommonMenuModelDishesType_cold:{
            sectionHeader.mainTitleLab.text = @"冷菜";
        } break;
        case V2CommonMenuModelDishesType_hot:{
            sectionHeader.mainTitleLab.text = @"热菜";
        } break;
        case V2CommonMenuModelDishesType_main:{
            sectionHeader.mainTitleLab.text = @"主食";
        } break;
        case V2CommonMenuModelDishesType_give:{
            sectionHeader.mainTitleLab.text = @"赠送";
        } break;
        default:
            break;
    }
    return sectionHeader;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.sectionArray.count > 0) {
        V2CommonMenuModel *cellModel = self.sectionArray[indexPath.section].array[indexPath.row];
        if (cellModel.dishesList.count > 0) {
            V2MenuCombCell *cell = [tableView dequeueReusableCellWithIdentifier:@"V2MenuCombCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.backgroundColor = UIColorHex(0xffffff);
            [cell fillWithModel:cellModel status:V2MenuSingleCellStatus_normal];
            return cell;
        } else {
            V2MenuSingleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"V2MenuSingleCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.bgView.hidden = YES;
            [cell fillWithModel:cellModel status:V2MenuSingleCellStatus_normal];
            return cell;
        }
    } else {
        V2MenuEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"V2MenuEmptyCellID"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        _tableview.backgroundColor =  UIColorHex(0xf7f7f7);
        _tableview.showsVerticalScrollIndicator = NO;
        
        [_tableview registerNib:[UINib nibWithNibName:@"V2MenuCombCell" bundle:nil] forCellReuseIdentifier:@"V2MenuCombCellID"];
        [_tableview registerNib:[UINib nibWithNibName:@"V2MenuSingleCell" bundle:nil] forCellReuseIdentifier:@"V2MenuSingleCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"V2MenuEmptyCell" bundle:nil] forCellReuseIdentifier:@"V2MenuEmptyCellID"];

        
    }
    return _tableview;
}
@end
